/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.abstractcanvas.client.gwt;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.MouseListener;
import com.google.gwt.user.client.ui.MouseListenerCollection;
import com.google.gwt.user.client.ui.MouseWheelListener;
import com.google.gwt.user.client.ui.MouseWheelListenerCollection;
import com.google.gwt.widgetideas.graphics.client.Color;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;

public class FocusGWTCanvas extends GWTCanvas 
{
	protected Color m_strokeColor = new Color("rgba(0,0,0,0)");
	
	private MouseListenerCollection mouseListeners;
	private MouseWheelListenerCollection mouseWheelListeners;
	private ClickListenerCollection clickListeners;

	public  FocusGWTCanvas() 
	{
		sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS | Event.ONMOUSEWHEEL);
	}

	public FocusGWTCanvas(int width, int height)
	{
		super(width, height);
		
		sinkEvents(Event.ONCLICK | Event.MOUSEEVENTS | Event.ONMOUSEWHEEL);
	}

	public void addClickListener(ClickListener listener) 
	{
		if (clickListeners == null) {
			clickListeners = new ClickListenerCollection();
		}
		clickListeners.add(listener);
	}
	public void addMouseListener(MouseListener listener) 
	{
		if (mouseListeners == null) {
			mouseListeners = new MouseListenerCollection();
		}
		mouseListeners.add(listener);
	}

	public void addMouseWheelListener(MouseWheelListener listener) 
	{
		if (mouseWheelListeners == null) {
			mouseWheelListeners = new MouseWheelListenerCollection();
		}
		mouseWheelListeners.add(listener);
	}

	public void removeClickListener(ClickListener listener) 
	{
		if (clickListeners != null) {
			clickListeners.remove(listener);
		}
	}
	
	public void removeMouseListener(MouseListener listener) 
	{
		if (mouseListeners != null) {
			mouseListeners.remove(listener);
		}
	}

	public void removeMouseWheelListener(MouseWheelListener listener) 
	{
		if (mouseWheelListeners != null) {
			mouseWheelListeners.remove(listener);
		}
	}

	@Override
	public void onBrowserEvent(Event event) 
	{
		switch (DOM.eventGetType(event)) 
		{
		case Event.ONCLICK:
			if (clickListeners != null) {
				clickListeners.fireClick(this);
			}
			break;

		case Event.ONMOUSEDOWN:
		case Event.ONMOUSEUP:
		case Event.ONMOUSEMOVE:
		case Event.ONMOUSEOVER:
		case Event.ONMOUSEOUT:
			if (mouseListeners != null) {
				mouseListeners.fireMouseEvent(this, event);
			}
			break;

		case Event.ONMOUSEWHEEL:
			if (mouseWheelListeners != null) {
				mouseWheelListeners.fireMouseWheelEvent(this, event);
			}
			break;
		}
	}

	@Override
	public void setStrokeStyle(Color color) {
		m_strokeColor = color;
		super.setStrokeStyle(color);
	}
	
	public Color getStrokeStyleColor()
	{
		return m_strokeColor;
	}

	
}
